package com.tac.se2_project2;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.UUID;

public class PasswordHasher {

	//this method will generate a hashed String for the given input string
		public static String md5(String input) {
	        
	        String md5 = null;
	         
	        if(null == input) return null;
	         
	        try {
	             
	        //Create MessageDigest object for MD5
	        MessageDigest digest = MessageDigest.getInstance("MD5");
	         
	        //Update input string in message digest
	        digest.update(input.getBytes(), 0, input.length());
	 
	        //Converts message digest value in base 16 (hex) 
	        md5 = new BigInteger(1, digest.digest()).toString(16);
	 
	        } catch (NoSuchAlgorithmException e) {
	 
	            e.printStackTrace();
	        }
	        return md5;
	    }
		
		public static String getSalt(){
			return UUID.randomUUID().toString();
		}
		
		/*public static Boolean CheckPassword(String email, String password){
			List<HashMap<String, Object>> results = null;
			try{
				results = Database.select("select * from Users where email = '" + email + "';");
			} catch (SQLException e) {
				e.printStackTrace();
			}
			if(results.size() == 0)
			{
				//no registered user with this login
				return false;
			}
			else{
				try{
					results = Database.select("select * from Users where email = '" + email + "' AND hashed_pw = '" + md5(password + results.get(0).get("salt")) + "';");
					//System.out.println(md5(password + results.get(0).get("salt")));
				} catch (SQLException e) {
					e.printStackTrace();
				}
				if(results.size() == 0)
				{
					//no registered user with this login
					return false;
				}
				else{
					LoggedInUser = new  User();
					LoggedInUser.Email = results.get(0).get("email").toString();
					LoggedInUser.RegisterDate = (java.util.Date) results.get(0).get("register_date");
					LoggedInUser.FirstName = results.get(0).get("first_name").toString();
					LoggedInUser.LastName = results.get(0).get("last_name").toString();
					LoggedInUser.Id = (int) results.get(0).get("id");
					LoggedInUser.isAdmin=LoggedInUser.Email.equals("admin");
					LoggedInUser.ProfilePicture = results.get(0).get("profile_picture").toString();
					LoggedInUser.BanEndDate = (java.util.Date) results.get(0).get("ban_end_date");
					return true;
				}
			}
		}*/
	
}
